package servlet;

import service.orcService.OrcService;
import service.orcService.OrcServiceImpl;
import util.Base46Util;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by wrh on 17-9-27./home/wrh/IdeaProjects/scannerAPI/target/classes/servlet/ImgServlet.class
 * 图片转文档API,只接受post请求
 */
public class ImgServlet extends javax.servlet.http.HttpServlet {

    public static final String POST_FORM_NAME_IMG = "img";
    OrcService orcService = new OrcServiceImpl();

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException {

        String img64=request.getParameter(POST_FORM_NAME_IMG);
        byte[] doc= new byte[0];
        try {
            doc = orcService.getDoc(Base46Util.decode(img64));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            response.getOutputStream().write(doc);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
